package com.ss.android.topic.d;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.AccelerateInterpolator;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.bytedance.common.utility.collection.WeakHandler;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ss.android.article.base.ui.LoadingFlashView;
import com.ss.android.article.base.ui.NoDataViewFactory$ImgType;
import com.ss.android.article.base.ui.ac;
import com.ss.android.article.base.ui.ad;
import com.ss.android.article.base.ui.NotifyViewHelper;
import com.ss.android.article.base.ui.ao;
import com.ss.android.article.browser.R;
import com.ss.android.article.common.page.PageList;
import com.ss.android.article.common.page.PageListObserver;
import com.ss.android.article.common.http.ApiError;
import com.ss.android.common.app.AbsFragment;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.newmedia.a.AbstractFooter;
import com.ss.android.topic.tips.TipsType;
import java.util.ArrayList;
import java.util.List;

public abstract class a extends AbsFragment implements WeakHandler.Callback, com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.a, PageListObserver {
    public ListView a;
    public AbstractFooter b;
    protected BaseAdapter c;
    public View d;
    public com.ss.android.topic.b.a e;
    protected PageList f;
    View mNotifyView;
    NotifyViewHelper h;
    WeakHandler i;
    Runnable j;
    private PullToRefreshListView k;
    private LoadingFlashView l;
    private com.ss.android.article.base.ui.NoNetView m;
    private TextView n;
    private boolean o;

    public a() {

        this.i = new WeakHandler(Looper.getMainLooper(), ((WeakHandler.Callback)this));
        this.j = new com.ss.android.topic.d.b(this);
        this.o = true;
    }

    protected static void a() {
    }

    public void a(AbsListView arg1, int arg2, int arg3, int arg4) {
    }

    public final void onError(boolean arg11, Throwable arg12) {
        String v0 = null;
        int v2 = R.string.ss_error_network_error;
        int v8 = 2;
        if(this.getActivity() != null && !this.getActivity().isFinishing()) {
            if(arg11) {
                if((arg12 instanceof ApiError)) {
                    v0 = ((ApiError)arg12).mErrorTips;
                }

                if(TextUtils.isEmpty(v0)) {
                    v0 = this.getResources().getString(v2);
                }

                if(this.i()) {
                    this.k.onRefreshComplete();
                }

                if(this.l != null) {
                    this.c();
                    if(!NetworkUtils.isNetworkAvailable(this.getActivity()) && (this.f.mItems.isEmpty())) {
                        this.d();
                        return;
                    }

                    if(!this.isViewValid()) {
                        return;
                    }

                    if(this.mNotifyView == null) {
                        return;
                    }

                    if((((String)v0)) == null) {
                        return;
                    }

                    this.i.removeCallbacks(this.j);
                    if((((String)v0)) != null) {
                        this.n.setText(v0);
                    }
                    else {
                        this.n.setText(0);
                    }

                    UIUtils.setVisibility(this.mNotifyView, 0);
                    NotifyViewHelper v0_2 = this.h;
                    View v1 = this.mNotifyView;
                    TextView v2_1 = this.n;
                    if(v2_1 != null && v1 != null) {
                        UIUtils.setVisibility(((View)v2_1), 0);
                        UIUtils.setVisibility(v1, 0);
                        ObjectAnimator v3 = ObjectAnimator.ofFloat(v1, "ScaleX", new float[]{0.95f, 1f});
                        ObjectAnimator v1_1 = ObjectAnimator.ofFloat(v1, "alpha", new float[]{0.7f, 1f});
                        ObjectAnimator v4 = ObjectAnimator.ofFloat(v2_1, "ScaleY", new float[]{0.8f, 1f});
                        ObjectAnimator v2_2 = ObjectAnimator.ofFloat(v2_1, "ScaleX", new float[]{0.8f, 1f});
                        v3.setInterpolator(new AccelerateInterpolator());
                        v2_2.setInterpolator(new ao());
                        v4.setInterpolator(new ao());
                        AnimatorSet v5 = new AnimatorSet();
                        Animator[] v6 = new Animator[v8];
                        v6[0] = v3;
                        v6[1] = v1_1;
                        v5.playTogether(v6);
                        AnimatorSet v1_2 = new AnimatorSet();
                        Animator[] v3_1 = new Animator[v8];
                        v3_1[0] = v2_2;
                        v3_1[1] = v4;
                        v1_2.playTogether(v3_1);
                        v5.setDuration(30);
                        v1_2.setDuration(300);
                        v5.start();
                        v1_2.start();
                        v0_2.b();
                    }

                    this.i.postDelayed(this.j, 2000);
                    return;
                }

                android.support.design.a.a(this.k, new TipsType[]{TipsType.LOADING});
                UIUtils.showImageToast(this.getActivity(), R.drawable.close_popup_textpage, ((String)v0));
            }
            else {
                this.b.switchToError(v2);
            }
        }
    }

    public void onStartLoading(boolean arg8, boolean arg9) {
        if(this.getActivity() != null && !this.getActivity().isFinishing()) {
            if((arg8) && (this.c.isEmpty())) {
                if(this.l != null) {
                    this.e();
                }
                else {
                    PullToRefreshListView v1 = this.k;
                    TipsType v0 = TipsType.LOADING;
                    com.ss.android.topic.tips.a v2 = v0.createTips(((View)v1).getContext());
                    if(v2 != null) {
                        int v3 = v0.ordinal();
                        ViewParent v0_1 = ((View)v1).getParent();
                        if(v0_1 == null) {
                        }
                        else if((v0_1 instanceof com.ss.android.topic.tips.b)) {
                            v2.a(((View)v1), ((ViewGroup)v0_1), v3);
                        }
                        else {
                            com.ss.android.topic.tips.b v4 = new com.ss.android.topic.tips.b(((View)v1).getContext());
                            ViewGroup.LayoutParams v5 = ((View)v1).getLayoutParams();
                            int v6 = ((ViewGroup)v0_1).indexOfChild(((View)v1));
                            ((ViewGroup)v0_1).removeViewAt(v6);
                            ((ViewGroup)v0_1).addView(((View)v4), v6, v5);
                            Drawable v0_2 = ((View)v1).getBackground();
                            if(v0_2 != null) {
                                TTUtils.a(((View)v4), v0_2);
                            }

                            v2.a(((View)v1), ((ViewGroup)v4), v3);
                        }
                    }
                    else {
                    }
                }

                return;
            }

            if(arg8) {
                return;
            }

            this.b.switchToLoading();
        }
    }

    public void b() {
        if((NetworkUtils.isNetworkAvailable(this.getActivity())) && this.m != null && this.m.getVisibility() == View.VISIBLE) {
            UIUtils.setVisibility(this.m, 8);
        }
    }

    public void onFinishLoading(boolean arg5, boolean arg6) {
        if(this.getActivity() != null && !this.getActivity().isFinishing()) {
            if((arg5) && (this.i())) {
                this.k.onRefreshComplete();
            }

            if(this.c.isEmpty()) {
                this.c();
                android.support.design.a.a(this.k, new TipsType[]{TipsType.LOADING});
            }

            this.b();
            if(!this.f.mHasMore) {
                this.b.switchToNoMoreContent(com.ss.android.article.browser.R.string.no_more_data);
            }

            if((this.c instanceof com.ss.android.k.a.b)) {
                BaseAdapter v0 = this.c;
                PageList v1 = this.f;
                ArrayList v2 = new ArrayList(v1.mItems.size());
                ((List)v2).addAll(v1.mItems);
                ((com.ss.android.k.a.b)v0).a(((List)v2));
            }
            else if(Logger.debug()) {
                throw new IllegalArgumentException("origin adapter must implement IPageListAdapter");
            }

            this.e.notifyDataSetChanged();
        }
    }

    public final void c() {
        UIUtils.setVisibility(this.l, 8);
    }

    public void d() {
        if(this.m == null) {
            this.m = android.support.design.a.a(this.getActivity(), this.getView(), ac.b(NoDataViewFactory$ImgType.NOT_NETWORK), ad.a(this.getString(com.ss.android.article.browser.R.string.not_network_tip)), null);
        }

        this.c();
        this.m.initNoDataActionBtn();
        this.m.setVisibility(View.VISIBLE);
    }

    public final void e() {
        if(this.l != null) {
            this.l.start();
        }
    }

    public void f() {
        if(this.i()) {
            this.k.setRefreshing(true);
        }
        else {
            this.f.refresh();
        }
    }

    public abstract BaseAdapter g();

    public abstract PageList h();

    public void handleMsg(Message message) {
    }

    public boolean i() {
        return true;
    }

    public final PageList j() {
        return this.f;
    }

    public View onCreateView(LayoutInflater arg2, ViewGroup arg3, Bundle arg4) {
        return android.support.design.a.b(arg3, R.layout.answer_list_fragment);
    }

    public void onDestroyView() {
        this.f.mObservers.remove(this);
        this.i.removeCallbacksAndMessages(null);
        if(this.h != null) {
            this.h.d();
        }

        super.onDestroyView();
    }

    public void onStop() {
        if(this.m != null) {
            this.m.b();
        }

        super.onStop();
    }

    public void onViewCreated(View view, Bundle bundle) {
        super.onViewCreated(view, bundle);
        this.mNotifyView = view.findViewById(com.ss.android.article.browser.R.id.notify_view);
        if(this.mNotifyView != null) {
            this.n = this.mNotifyView.findViewById(com.ss.android.article.browser.R.id.notify_view_text);
        }

        this.l = view.findViewById(com.ss.android.article.browser.R.id.empty_load_view);
        this.k = view.findViewById(com.ss.android.article.browser.R.id.list);
        this.a = this.k.getRefreshableView();
        this.h = new NotifyViewHelper(view.getContext());
        this.a.addHeaderView(this.h.a());
        this.k.setOnViewScrollListener(((com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.a)this));
        this.d = android.support.design.a.b(this.a, R.layout.h7);
        this.b = new c(this, this.d.findViewById(com.ss.android.article.browser.R.id.ss_footer_content));
        this.b.hideFooterContent();
        this.c = this.g();
        this.e = new com.ss.android.topic.b.a(this.c);
        com.ss.android.topic.b.a v0 = this.e;
        View v1 = this.d;
        if(!v0.a.contains(v1)) {
            v0.a.add(v1);
            v0.notifyDataSetChanged();
        }

        this.a.setAdapter(this.e);
        this.f = this.h();
        this.f.mObservers.add(this);
        this.a.setOnScrollListener(new com.ss.android.topic.d.d(this));
        if(this.i()) {
            this.k.setOnRefreshListener(new e(this));
        }
        else {
            this.k.setMode(Mode.DISABLED);
        }

        if(this.o) {
            this.f();
        }
    }

    public void onViewScrollChanged(int arg3, int arg4, int arg5, int arg6) {
        if(this.h != null) {
            this.h.a(this.k, arg4, arg6);
        }
    }
}

